package com.wyu.controller;


import com.wyu.pojo.Page;
import com.wyu.pojo.User;
import com.wyu.pojo.menu;
import com.wyu.service.MenuService;
import com.wyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/manager123")
public class ManagerController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @Autowired
    @Qualifier("MenuServiceImpl")
    private MenuService menuService;

    @RequestMapping("/tomanager")
    public String homepage() {
        return "manager/manager";
    }

    @RequestMapping("/menu_manager/{pageNo}")
    public String menu_manager(Model model,@PathVariable int pageNo) {
        Page<menu> page = menuService.page(pageNo,Page.PAGE_SIZE);
        page.setUrl("manager123/menu_manager");
        model.addAttribute("page",page);
        return "manager/menu_manager";
    }

    @RequestMapping("/toaddmenu")
    public String addPaper() {
        return "manager/menu_add";
    }

    @RequestMapping("/addmenu")
    public String toUpdateBook(menu menu) {
        menuService.addMenu(menu);
        return "redirect:/manager123/menu_manager/1";
    }

    @RequestMapping("/toupdatemenu/{id}")
    public String updateBook(Model model, @PathVariable int id) {
        menu menu = menuService.queryMenuById(id);
        System.out.println(menu);
        model.addAttribute("menu",menu );
        return "manager/menu_update";
    }
    @RequestMapping("/updatemenu")
    public String deleteBook(Model model, menu menu) {
        System.out.println(menu);
        menuService.updateMenu(menu);
        return "redirect:/manager123/menu_manager/1";
    }

    @RequestMapping("/deletemenu/{id}")
    public String deleteBook(@PathVariable int id) {
        menuService.deleteMenuById(id);
        return "redirect:/manager123/menu_manager/1";
    }

    @RequestMapping("/toregistmanager")
    public String toregistmanager() {
        return "manager/registmanager";
    }

    @RequestMapping("/registmanager")
    public String registmanager(Model model, String username, String password, String email) {
//        3、检查 用户名是否可用
        if (userService.existsUsername(username)) {
            System.out.println("用户名[" + username + "]已存在!");

            // 把回显信息，保存到Request域中
            model.addAttribute("msg", "用户名已存在！！");
            model.addAttribute("username", username);
            model.addAttribute("email", email);

//        跳回注册页面
            return "manager/registmanager";
        } else {
            //      可用
//                调用Sservice保存到数据库
            userService.registUser(new User(null, username, password, email,2));
//
//        跳到注册成功页面 regist_success.jsp
            return "manager/regist_success";
        }
    }
}